package com.hrd.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hrd.common.util.RestTemplateUtil;
import com.hrd.common.util.http.HttpDeal;
import com.hrd.mybatis.bean.ext.JDResult;
import com.hrd.service.CommonService;
import com.hrd.service.KingDeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 刘江宁
 * @create 2022/1/13 9:07
 */
@Service
@Slf4j
public class KingDeeServiceImpl implements KingDeeService {
    @Autowired
    private CommonService commonService;


    public String getKingDeeInterfacePost(String payload, String url) {

        Map<String, String> headers = new HashMap<>();
        JDResult jdResult = commonService.authToken();

        //设置请求头使用add方法
        headers.put("Content-Type", "application/json;charset=UTF-8");
        headers.put("groupName", jdResult.getGroupName());
        headers.put("accountId", jdResult.getAccountId());
        headers.put("X-GW-Router-Addr", jdResult.getDomain());

        HashMap params = JSONObject.parseObject(payload, HashMap.class);
        String post = HttpDeal.post(url, headers, params);
        System.out.println(post);
        return post;
    }


    @Override
    public String getKindDeeCustomerId(String customerNo,String token) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("number", customerNo);

        String url = "http://api.kingdee.com/jdy/basedata/customer_detail?access_token=" + token;
        String resultStr = getKingDeeInterfacePost(jsonObject.toString(), url);
        log.info("=======调用金蝶传递参数=> url:{}== param:{}========",url,jsonObject.toString());
        log.info("=======金蝶返回数据:{}========",resultStr);

        JSONObject kingDeeResultObj = JSONObject.parseObject(resultStr);

        JSONObject data = kingDeeResultObj.getJSONObject("data");


        return data.getString("id");
    }

    @Override
    public String getKindDeeCustomerIdByName(String customerName, String token) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("search", customerName);
        String url = "http://api.kingdee.com/jdy/basedata/customer_list?access_token=" + token;

        String resultStr = getKingDeeInterfacePost(jsonObject.toString(), url);
        log.info("=======调用金蝶传递参数=> url:{}== param:{}========",url,jsonObject.toString());
        log.info("=======金蝶返回数据:{}========",resultStr);
        JSONObject kingDeeResultObj = JSONObject.parseObject(resultStr);
        JSONArray jsonArray = kingDeeResultObj.getJSONObject("data").getJSONArray("rows");
        if (jsonArray==null || jsonArray.size()==0){
            return null;
        }
        List<Map> name = jsonArray.toJavaList(Map.class).stream().filter(o -> customerName.equals(o.get("name"))).collect(Collectors.toList());
        if (name==null || name.size()==0){
            return null;
        }
        Map map = name.get(0);
        return map.get("id").toString();
    }

    @Override
    public JSONObject getKindDeeMaterial(String machineType, String token) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("search", machineType);
        jsonObject.put("pagesize", 100);
        String url = "http://api.kingdee.com/jdy/basedata/material_list?access_token=" + token;

         String resultStr = getKingDeeInterfacePost(jsonObject.toString(), url);

        log.info("=======调用金蝶传递参数=> url:{}== param:{}========",url,jsonObject.toString());
        log.info("=======金蝶返回数据:{}========",resultStr);
        JSONObject kingDeeResultObj = JSONObject.parseObject(resultStr);
        JSONArray jsonArray = kingDeeResultObj.getJSONObject("data").getJSONArray("rows");
        List<Map> list = jsonArray.toJavaList(Map.class);

        if (CollectionUtil.isEmpty(list)){
            return null;
        }
        Map map = null;
        for (Map model : list) {
            if (machineType.equals((String)model.get("number")) || machineType.equals((String)model.get("name")) || machineType.equals((String) model.get("model"))){
                map = model;
            }
        }
        if (map==null){
            return null;
        }


        //根据id 查出商品详情
        String url2 = "http://api.kingdee.com/jdy/basedata/material_detail?access_token=" + token;
        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("id", map.get("id"));

        String resultStr2 = getKingDeeInterfacePost(jsonObject2.toString(), url2);
        log.info("=======调用金蝶传递参数=> url:{}== param:{}========",url2,jsonObject2.toString());
        log.info("=======金蝶返回数据:{}========",resultStr2);
        JSONObject kingDeeResultObj2 = JSONObject.parseObject(resultStr2);
        JSONObject data = kingDeeResultObj2.getJSONObject("data");

        return data;


    }

    @Override
    public JSONObject getauxInfo(String machineType, String token) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", machineType);
        String url = "http://api.kingdee.com/jdy/basedata/auxInfo_list?access_token=" + token;

        String resultStr = getKingDeeInterfacePost(jsonObject.toString(), url);

        log.info("=======调用金蝶传递参数=> url:{}== param:{}========",url,jsonObject.toString());
        log.info("=======金蝶返回数据:{}========",resultStr);
        JSONObject kingDeeResultObj = JSONObject.parseObject(resultStr);
        JSONArray jsonArray = kingDeeResultObj.getJSONObject("data").getJSONArray("rows");
        List<JSONObject> list = jsonArray.toJavaList(JSONObject.class);
        JSONObject map = list.get(0);
        for (JSONObject model : list) {
            if (machineType.equals(model.get("name"))){
                map = model;
            }
        }



        return map;


    }

    @Override
    public List<Map> getKindDeeEmpId(String empName,  String token) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("search", empName);
        String url = "http://api.kingdee.com/jdy/basedata/emp_list?access_token=" + token;

        String resultStr = getKingDeeInterfacePost(jsonObject.toString(), url);
        log.info("=======调用金蝶传递参数=> url:{}== param:{}========",url,jsonObject.toString());
        log.info("=======金蝶返回数据:{}========",resultStr);
        JSONObject kingDeeResultObj = JSONObject.parseObject(resultStr);
        JSONArray jsonArray = kingDeeResultObj.getJSONObject("data").getJSONArray("rows");
        List<Map> name = jsonArray.toJavaList(Map.class).stream().filter(o -> empName.equals(o.get("name"))).collect(Collectors.toList());
        return name;
    }

    @Override
    public JSONObject getKingDeeOutbound(String number,  String token) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("number", number);
        String url = "http://api.kingdee.com/jdy/sal/sal_outbound_detail?access_token=" + token;

        String resultStr = getKingDeeInterfacePost(jsonObject.toString(), url);
        log.info("=======调用金蝶传递参数=> url:{}== param:{}========",url,jsonObject.toString());
        log.info("=======金蝶返回数据:{}========",resultStr);
        JSONObject kingDeeResultObj = JSONObject.parseObject(resultStr);

        JSONObject data = kingDeeResultObj.getJSONObject("data");


        return data;
    }

    @Override
    public JSONObject getKingAuxinfo(String number, String token) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("number", number);
        String url = "http://api.kingdee.com/jdy/basedata/auxinfo_detail?access_token=" + token;

        String resultStr = getKingDeeInterfacePost(jsonObject.toString(), url);
        log.info("=======调用金蝶传递参数=> url:{}== param:{}========",url,jsonObject.toString());
        log.info("=======金蝶返回数据:{}========",resultStr);
        JSONObject kingDeeResultObj = JSONObject.parseObject(resultStr);

        JSONObject data = kingDeeResultObj.getJSONObject("data");


        return data;
    }

    public static void main(String[] args) {

        String ttt = "{\n" +
                "    \"success\": true,\n" +
                "    \"reachPlugin\": true,\n" +
                "    \"data\": {\n" +
                "        \"totalpage\": 1,\n" +
                "        \"curpagesize\": 1,\n" +
                "        \"count\": 1,\n" +
                "        \"pagesize\": 10,\n" +
                "        \"page\": 1,\n" +
                "        \"rows\": [\n" +
                "            {\n" +
                "                \"number\": \"ZY00009\",\n" +
                "                \"enable\": \"1\",\n" +
                "                \"idnumber\": \"\",\n" +
                "                \"name\": \"韩宜千\",\n" +
                "                \"mobile\": \"\",\n" +
                "                \"id\": \"1333361909956668416\"\n" +
                "            }\n" +
                "        ]\n" +
                "    },\n" +
                "    \"errorCode\": \"success\"\n" +
                "}";

        JSONObject kingDeeResultObj = JSONObject.parseObject(ttt);
        JSONArray jsonArray = kingDeeResultObj.getJSONObject("data").getJSONArray("rows");
        Map map = jsonArray.toJavaList(Map.class).stream().filter(o -> "韩宜千".equals(o.get("name"))).findFirst().get();



        System.out.println(map.get("id").toString());
    }



}
